module.exports = function ($scope, $modalInstance, $resource, $modal, item, flag, toaster){





    //查看
    $scope.getInfo = function(info) {
        var para = {
            'id' : info.id
            }
         $resource('/api/as/tc/ticketStyle/getInfo', {}, {}).save(para, function(res){
             if(res.errcode === 0){
                $scope.info = res.data;
                if($scope.info.ticket_name === "true"){
                    $scope.info.ticket_name = true
                }else{
                    $scope.info.ticket_name = false
                }
                if($scope.info.pay_price == "true"){
                    $scope.info.pay_price = true
                }else{
                    $scope.info.pay_price = false
                }
                if($scope.info.user_num == "true"){
                    $scope.info.user_num = true
                }else{
                    $scope.info.user_num = false
                }
                if($scope.info.order_code == "true"){
                    $scope.info.order_code = true
                }else{
                    $scope.info.order_code = false
                }
                if($scope.info.voucher_code == "true"){
                    $scope.info.voucher_code = true
                }else{
                    $scope.info.voucher_code = false
                }
                if($scope.info.create_order_time == "true"){
                    $scope.info.create_order_time = true
                }else{
                    $scope.info.create_order_time = false
                }
                if($scope.info.contact_user == "true"){
                    $scope.info.contact_user = true
                }else{
                    $scope.info.contact_user = false
                }
                if($scope.info.contact_info == "true"){
                    $scope.info.contact_info = true
                }else{
                    $scope.info.contact_info = false
                }
                if($scope.info.QR_code == "true"){
                    $scope.info.QR_code = true
                }else{
                    $scope.info.QR_code = false
                }
                if($scope.info.counter_code == "true"){
                    $scope.info.counter_code = true
                }else{
                    $scope.info.counter_code = false
                }
                if($scope.info.device_code == "true"){
                    $scope.info.device_code = true
                }else{
                    $scope.info.device_code = false
                }
             }else{
                 toaster.error({title: '', body: res.errmsg});
             }
         });
    }


    //景区列表
    $scope.findPlaceList = function(info) {
         $resource('/api/as/tc/place/getPlaceCodeList', {}, {}).save({}, function(res){
            if(res.errcode === 0){
                $scope.viewlist  = res.data;
             }else{
                 toaster.error({title: '', body: res.errmsg});
             }
         });
    }


    
    init();
    function init(){
        $scope.info = flag ? {'flag': true} : angular.copy(item);
        $scope.findPlaceList();
        if(flag){
            // $scope.info.format_height = 0;
            // $scope.info.format_width = 0;        
        }
        if(!flag){
            $scope.getInfo($scope.info);
            console.log($scope.info)
            
        }
    }

        

    //保存
    $scope.save = function (){
        console.log($scope.info)
        if($scope.info.ticket_name == true){
            $scope.info.ticket_name = "true";
            if($scope.info.ticket_name_x == undefined || $scope.info.ticket_name_y == undefined ){
                    alert("打印位置坐标不能为空！");
                    return;
            }
        }else{
            $scope.info.ticket_name = "false"
        }
        if($scope.info.pay_price == true){
            $scope.info.pay_price = "true";
            if($scope.info.pay_price_x == undefined || $scope.info.pay_price_y == undefined ){
                alert("打印位置坐标不能为空！");
                return;
        }
        }else{
            $scope.info.pay_price = "false"
        }
        if($scope.info.user_num == true){
            $scope.info.user_num = "true";
            if($scope.info.user_num_x == undefined || $scope.info.user_num_y == undefined ){
                alert("打印位置坐标不能为空！");
                return;
        }
        }else{
            $scope.info.user_num = "false"
        }
        if($scope.info.order_code == true){
            $scope.info.order_code = "true";
            if($scope.info.order_code_x == undefined || $scope.info.order_code_y == undefined ){
                alert("打印位置坐标不能为空！");
                return;
        }
        }else{
            $scope.info.order_code = "false"
        }
        if($scope.info.voucher_code == true){
            $scope.info.voucher_code = "true";
            if($scope.info.voucher_code_x == undefined || $scope.info.voucher_code_y == undefined ){
                alert("打印位置坐标不能为空！");
                return;
        }
        }else{
            $scope.info.voucher_code = "false"
        }
        if($scope.info.create_order_time == true){
            $scope.info.create_order_time = "true";
            if($scope.info.create_order_time_x == undefined || $scope.info.create_order_time_y == undefined ){
                alert("打印位置坐标不能为空！");
                return;
        }
        }else{
            $scope.info.create_order_time = "false"
        }
        if($scope.info.contact_user == true){
            $scope.info.contact_user = "true";
            if($scope.info.contact_user_x == undefined || $scope.info.contact_user_y == undefined ){
                alert("打印位置坐标不能为空！");
                return;
        }
        }else{
            $scope.info.contact_user = "false"
        }
        if($scope.info.contact_info == true){
            $scope.info.contact_info = "true";
            if($scope.info.contact_info_x == undefined || $scope.info.contact_info_y == undefined ){
                alert("打印位置坐标不能为空！");
                return;
        }
        }else{
            $scope.info.contact_info = "false"
        }
        if($scope.info.QR_code == true){
            $scope.info.QR_code = "true";
            if($scope.info.QR_code_x == undefined || $scope.info.QR_code_y == undefined || $scope.info.QR_code_height == undefined ){
                alert("打印位置坐标或高度不能为空！");
                return;
        }
        }else{
            $scope.info.QR_code = "false"
        }
        if($scope.info.counter_code == true){
            $scope.info.counter_code = "true";
            if($scope.info.counter_code_x == undefined || $scope.info.counter_code_y == undefined ){
                alert("柜台号X轴Y轴不能为空！");
                return;
        }
        }else{
            $scope.info.counter_code = "false"
        }
        if($scope.info.device_code == true){
            $scope.info.device_code = "true";
            if($scope.info.device_code_x == undefined || $scope.info.device_code_y == undefined ){
                alert("设备号X轴Y轴不能为空！");
                return;
        }
        }else{
            $scope.info.device_code = "false"
        }
        $resource('/api/as/tc/ticketStyle/save', {}, {}).save($scope.info, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '保存成功'});
                $modalInstance.close();
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }
   
    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }

};